package com.example.mylitepal;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.app.Activity;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.mylitepal.model.Song;

import org.litepal.LitePal;

public class MainActivity extends AppCompatActivity  {
private Button button1,button2,button3,button4;
    private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityCompat.requestPermissions(this,PERMISSIONS_STORAGE,1);
        //SQLiteDatabase db = LitePal.getDatabase();
        initView();
    }
    public void initView(){
        button1=findViewById(R.id.btn1);
        button2=findViewById(R.id.btn2);
        button3=findViewById(R.id.btn3);
        button4=findViewById(R.id.btn4);
        button1.setOnClickListener(new View.OnClickListener() {
           @Override
            public void onClick(View v) {
                Song song = new Song();
                song.setName("girl");
                song.setDuration(50);
                song.setSize("20M");
                if (song.save()) {
                    Toast.makeText(MainActivity.this, "存储成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "存储失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              LitePal.deleteAll(Song.class,"duration>?", "35");
              Toast.makeText(MainActivity.this, "删除Song表中duration>35数据", Toast.LENGTH_SHORT).show();
            }
        });
        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Song song = new Song();
                //更新内容
                song.setDuration(35);
                song.setSize("100M");
                //更新所有满足的判读条件
                song.updateAll("duration=? and size=?","50","20M");
                Toast.makeText(MainActivity.this, "删除Song表中duration>35数据", Toast.LENGTH_SHORT).show();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               //查表中最后一条数据
                Song song = LitePal.findLast(Song.class);
                if(song==null){
                    Toast.makeText(MainActivity.this, "表中没有数据", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this, "最后一条数据Name:"+song.getName(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
